package com.zhiyi.xppay.main;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;

import com.zhiyi.xppay.consts.AppConstsNxys;
import com.zhiyi.xppay.hook.CloudPayHook;
import com.zhiyi.xppay.hook.HookNxys;
import com.zhiyi.xppay.utils.L;
import com.zhiyi.xppay.utils.PayHelperUtils;
import com.zhiyi.xppay.utils.ToolsNxys;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * @author 大连致一科技
 * @ClassName: Main
 * @Description: 开始监控所有
 * @date 2018年11月7日 下午1:26:26
 */
public class Main implements IXposedHookLoadPackage {
    public static String QQ_PACKAGE = "com.tencent.mobileqq";
    public static String CP_PACKAGE = "com.unionpay";

    public static boolean CP_PACKAGE_ISHOOK = false;
    private static boolean ishookednxys = false;

    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam)
            throws Throwable {
        if (lpparam.appInfo == null || (lpparam.appInfo.flags & (ApplicationInfo.FLAG_SYSTEM |
                ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
            if (lpparam.appInfo != null) {
                L.d("不能监控的包" + lpparam.appInfo.name + "," + lpparam.packageName);
            }
            return;
        }
        final String packageName = lpparam.packageName;
        final String processName = lpparam.processName;
        if (CP_PACKAGE.equals(packageName)) {
            L.d("开始监控云闪付");
            XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
                    Context context = (Context) param.args[0];
                    ClassLoader appClassLoader = context.getClassLoader();
                    if (CP_PACKAGE.equals(processName) && !CP_PACKAGE_ISHOOK) {
                        CP_PACKAGE_ISHOOK = true;
                        PayHelperUtils.sendmsg(context, " 云闪付hook成功,当前版本 " + PayHelperUtils.getVerName(context));
                        new CloudPayHook().hook(lpparam.classLoader, context);
                    }
                }
            });
        } else if (AppConstsNxys.PACKAGENAME.equals(packageName)) {
            L.d("开始监控农信易扫");
            XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
                    L.d("activity name >>>>>>>>>>>>>>>>>" + param.thisObject.getClass().getName());
                    if (param.thisObject.getClass().getName().equals("com.buybal.buybalpay.activity.HomeActivity") && !ishookednxys) {
                        ishookednxys = true;
                        ClassLoader classLoader = param.thisObject.getClass().getClassLoader();
                        Activity activity = (Activity) param.thisObject;
                        ReceiverNxys receiverNxys = new ReceiverNxys();
                        IntentFilter filter = new IntentFilter();
                        filter.addAction(AppConstsNxys.ACTION_CREATEQRCODE);
                        filter.addAction(AppConstsNxys.ACTION_TRADEQUERY);
                        filter.addAction(AppConstsNxys.ACTION_ACTIVITYSTART);
                        activity.registerReceiver(receiverNxys, filter);
                        new HookNxys().Hook(classLoader, (Context) param.thisObject);
                        ToolsNxys.ishookeed = true;
                    }
                }
            });
        }
    }

    private class ReceiverNxys extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(AppConstsNxys.ACTION_CREATEQRCODE)) {
                String money = intent.getStringExtra("money");
                String type = intent.getStringExtra("type");
                String mark = intent.getStringExtra("mark");
                ToolsNxys.CreateQRCode(money, mark, type);
            } else if (intent.getAction().equals(AppConstsNxys.ACTION_TRADEQUERY)) {
                L.d("查詢訂單");
                HookNxys.getalltrade();
            } else if (intent.getAction().equals(AppConstsNxys.ACTION_ACTIVITYSTART)) {
                if (!ToolsNxys.isactivitystart) {
                    ToolsNxys.isactivitystart = true;
                    ToolsNxys.tradequery(context);
                }
            }
        }
    }


//    public class CloundPayReceiver extends BroadcastReceiver {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent.getAction().equals(AppConst.CP_QRCODE)) {
//                L.d("生产云闪付二维码");
//                String money = intent.getStringExtra("money");
//                String mark = intent.getStringExtra("mark");
//                if (money == null) {
//                    money = "0.01";
//                }
//                if (mark == null) {
//                    mark = "测试";
//                }
//                final Activity activity = (Activity) context;
//                CloudPayHook.BuildQRcode(activity, money, mark);
//            } else if (intent.getAction().equals(AppConst.CP_GETPAYER)) {
//                String orderid = intent.getStringExtra("orderid");
//                CloudPayHook.startGetPayer("mark", orderid);
//            } else if (intent.getAction().equals(AppConst.CP_LOGIN)) {
//                CloudPayHook.Login();
//            }
//        }
//    }


}
